package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.meta.business.client.api.ElevenDimensionsBusinessCardClient;
import com.meta.business.client.resp.GetUserBusinessCardReq;
import com.meta.business.client.resp.GetUserBusinessCardRes;
import com.meta.component.base.api.CommonResult;
import com.sc.nft.dao.BusinessCardOrderDao;
import com.sc.nft.dao.UserBusinessCardDao;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.JudgeUserIsActivationBusinessCardVO;
import com.sc.nft.enums.OrderStatusEnum;
import com.sc.nft.service.UserBusinessCardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
* @author zhangchao
* @description 针对表【sc_user_business_card(用户名片信息)】的数据库操作Service实现
* @createDate 2024-07-08 18:30:37
*/
@Slf4j
@RequiredArgsConstructor
@Service
public class UserBusinessCardServiceImpl implements UserBusinessCardService {
    
    private final UserBusinessCardDao userBusinessCardDao;
    private final BusinessCardOrderDao businessCardOrderDao;
    private final ElevenDimensionsBusinessCardClient elevenDimensionsBusinessCardClient;


    @Override
    public JudgeUserIsActivationBusinessCardVO judgeUserIsActivationBusinessCard(UserInfo userInfo) {
        JudgeUserIsActivationBusinessCardVO result= new JudgeUserIsActivationBusinessCardVO();
        result.setLiberal(userBusinessCardDao.judgeUserIsActivationBusinessCard(userInfo.getId()));
        // 获取用户是订单信息.如果说他已经开通了,那么就不告知前端 如果没开通,那么查询用户的待支付订单信息,看是否有待支付的订单存在.如果也没有的话,那么就证明他没有下单.
        if (!result.getLiberal()) {
            result.setCreateOrder(CollUtil.isNotEmpty(businessCardOrderDao.getUserHasAnyUnpaidOrders(userInfo.getId(), OrderStatusEnum.WAIT_PAY)));
        }
        GetUserBusinessCardReq getUserBusinessCardReq=new GetUserBusinessCardReq();
        getUserBusinessCardReq.setOwner(userInfo.getUserUuid());
        CommonResult userBusinessCard = elevenDimensionsBusinessCardClient.getUserBusinessCard(getUserBusinessCardReq);
        // 请求第三方,如果不是200的话那就证明没开通
        if (userBusinessCard.getCode() != 200L) {
            result.setCreateBusinessCard(Boolean.FALSE);
        } else {
            // 列表中如果不存在也证明是没开通
            if (BeanUtil.isEmpty(userBusinessCard.getData())) {
                result.setCreateBusinessCard(Boolean.FALSE);
            } else {
                GetUserBusinessCardRes getUserBusinessCardRes = Convert.convert(GetUserBusinessCardRes.class, userBusinessCard.getData());
                if (getUserBusinessCardRes.getId()!=null) {
                    result.setCreateBusinessCard(Boolean.TRUE);
                }else {
                    result.setCreateBusinessCard(Boolean.FALSE);
                }

            }
        }
        return result;
    }
}




